package com.example.dellpc.myapplication;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.TextView;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.syd.oden.gesturelock.view.GestureLockViewGroup;
import com.syd.oden.gesturelock.view.listener.GestureEventListener;
import com.syd.oden.gesturelock.view.listener.GesturePasswordSettingListener;
import com.syd.oden.gesturelock.view.listener.GestureUnmatchedExceedListener;

/**
 * Created by 刘智强 on 2016/8/30.
 */
public class GestureLockActivity extends Activity {
    @ViewInject(R.id.gesturelock)
    private GestureLockViewGroup gestureLockViewGroup;
    @ViewInject(R.id.tv_state)
    private TextView tv_state;
    @ViewInject(R.id.btn_getpw)
    private Button btn_getpw;
    @ViewInject(R.id.btn_clear)
    private Button btn_clear;

    /**
     * 第一次设置成功后自动保存到本地，  再上传到服务器，以后每次判断就先判断本地是否有密码 ，再作密码的匹配判断。
     * @param savedInstanceState
     */

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gesturelockactivity);
        ViewUtils.inject(this);
        init();
    }
    @OnClick({R.id.btn_getpw,R.id.btn_clear})
    public  void onClick(Button btn){
        switch (btn.getId()){
            case R.id.btn_getpw:
                Log.e("GestureLockActivity",gestureLockViewGroup.getPassword());//获取密码
                break;
            case  R.id.btn_clear:
                gestureLockViewGroup.resetView();//清楚密码
                break;
        }
    }

    public void init() {

        if (gestureLockViewGroup.isSetPassword()){
            tv_state.setTextColor(Color.GREEN);
            tv_state.setText("请解锁!!!");
        }else {
            tv_state.setTextColor(Color.RED);
            tv_state.setText("请先绘制手势密码!!!");
        }
        //解锁的回调
        gestureLockViewGroup.setGestureEventListener(new GestureEventListener() {
            @Override
            public void onGestureEvent(boolean matched) {
                if (!matched) {
                    tv_state.setTextColor(Color.RED);
                    tv_state.setText("手势密码错误");
                } else {
//                    if (isReset) {
//                        isReset = false;
//                        Toast.makeText(MainActivity.this, "清除成功!", Toast.LENGTH_SHORT).show();
//
//                    } else {
                    tv_state.setTextColor(Color.BLUE);
                    tv_state.setText("手势密码正确");
//                    }
                }
            }
        });
        gestureLockViewGroup.setGesturePasswordSettingListener(new GesturePasswordSettingListener() {
            @Override
            public boolean onFirstInputComplete(int i) {
                //判断至少要连接多少个点
                if (i > 3) {
                    tv_state.setText("再次绘制手势密码!!!");
                    tv_state.setTextColor(Color.BLUE);
                    return true;
                } else {
                    tv_state.setText("至少连接4个点!!!");
                    tv_state.setTextColor(Color.RED);
                    return false;
                }

            }

            @Override
            public void onSuccess() {
                tv_state.setText("请输入手势密码解锁!!!");
                tv_state.setTextColor(Color.BLUE);
                Utils.toast(getApplicationContext(), "手势密码设置成功!!!");
            }

            @Override
            public void onFail() {
                tv_state.setTextColor(Color.RED);
                tv_state.setText("与上一次绘制的手势密码不一致，请重新绘制");

            }
        });
        //解锁3次错误   不能再解锁
        gestureLockViewGroup.setGestureUnmatchedExceedListener(3, new GestureUnmatchedExceedListener() {
            @Override
            public void onUnmatchedExceedBoundary() {
                tv_state.setTextColor(Color.RED);
                tv_state.setText("错误次数过多，请稍后再试!");
            }
        });


    }
}
